package com.it.xml;

import com.it.pojo.PageBean;
import com.it.pojo.Student;
import com.it.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin    // 允许其他源、访问
@Controller
@RequestMapping("/stu")
@ResponseBody
public class StudentController {
    @Autowired
    private StudentService studentService;

    public Map<String, Object> resInfo(int i, String val) {
        Map<String, Object> res = new HashMap<>();
        if (i > 0) {
            res.put("code", 200);
            res.put("data", true);
            res.put("msg", val + "成功");
        } else {
            res.put("code", 404);
            res.put("data", false);
            res.put("msg", val + "失败！");
        }
        return res;
    }

    // 控制层（查询学生全部信息）
    @GetMapping("/all")
    public PageBean<Student> all(
            @RequestParam(value = "index", required = true) int index,
            @RequestParam(value = "size",required = false,defaultValue = "10") int size
    ) {
        return studentService.queryAllService(index, size);
    }

    // 控制层 (根据年龄查询)
    @GetMapping("/age")
    public List<Student> age(int ageNum) {
        List<Student> students = studentService.queryAgeService(ageNum);
        return students;
    }

    // 控制层 (根据性别查询)
    @GetMapping("/gender")
    public List<Student> age(String genderNum) {
        List<Student> students = studentService.queryGenderService(genderNum);
        return students;
    }

    // 控制层（新增一条学生信息）
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody Student student) {
        int i = studentService.addstuService(student);
        Map<String, Object> res = new HashMap<>();
        return this.resInfo(i, "添加");
    }

    // 控制层（根据 id 删除一条数据）
    @DeleteMapping("/delete")
    public Map<String, Object> delete(int id) {
        int i = studentService.delstuService(id);
        return this.resInfo(i, "删除");
    }

    // 控制层（根据id修改）
    @PutMapping("/put")
    public Map<String, Object> put(@RequestBody Student student) {
        int i = studentService.putstuService(student);
        return this.resInfo(i, "修改");
    }
}
